﻿var lastTitle = "";
var sharesRoot = null;
var sharesTree = null;
var sharesVideoRoot = null;
var sharesVideoTree = null;
var playlistRoot = null;
var playlistTree = null;
var currentDirectorySelected = null;
var isMuted = false;
var currentVolume = "50";
var soundSlider = null;
var songProgressBar = null;
var currentlyPlaying = new Object();
var singleCmdToken = false;
var currentPlaylist = 'music';
var tabs2 = null;
var connectStatus = 'Connected';
var Tree = Ext.tree;
var newNode = null;
var nodeId = 0;
var currentNodeSelected = 0;
var navButtonPlay = null;

// Start a simple clock task that updates currently playing info every 5 seconds
var currentlyPlayingTask = {
    run: function() {
        if (playlistRoot != null && playlistRoot != undefined) {
            GetCurrentlyPlaying();
        }
    },
    interval: 1500 //1.5 seconds
}

/********************* musicSharesSelected ***********************/

function musicSharesSelected() {
    var ele = document.getElementById("myTreeVideo");
    ele.style.display = "none";

    ele = document.getElementById("myTreeMusic");
    ele.style.display = "block";

    currentPlaylist = 'music';

}

/********************* connectToMediaServer ***********************/

function connectToMediaServer() {

    InitializeSharesTree();
    InitializePlaylistArea();
    InitializeWidgets();
    setPlaylistType(currentPlaylist);
    hideGuiControl();
    Ext.TaskMgr.start(currentlyPlayingTask);
}

function expandnodeMusic(node) {
    currentPlaylist = 'music';
    if (!node.attributes.id != "sharesRoot") {
        getDirectoryInfo(node);
    }
}

function expandnodeVideo(node) {
    currentPlaylist = 'video';
    if (!node.attributes.id != "sharesRoot") {
        getDirectoryInfo(node);
    }
}

function InitializeWidgets() {
    soundSlider = new Ext.Slider({
        renderTo: 'sound-slider',
        width: 190,
        minValue: 0,
        maxValue: 100,
        plugins: new Ext.ux.SliderTip()
    });
    soundSlider.on('dragend', function(e) {
        buttonSetVolume(e.value);
        currentVolume = e.value;
        if (currentVolume == 0)
            isMuted = true;
        else
            isMuted = false;
        //changeMuteButton(isMuted);
    });

    songProgressBar = new Ext.ProgressBar({
        height: 7,
        text: 'Waiting',
        id: 'songProgressBar',
        textEl: 'songProgressText',
        cls: 'custom',
        renderTo: 'songProgressBar'
    });

    navButtonPlay = new Ext.Button({ renderTo: 'buttonPlay',
        icon: 'images/navigator_play.png',
        scale: 'large'
    });
    navButtonPlay.addListener('click', buttonPlay);

    navButtonPause = new Ext.Button({ renderTo: 'buttonPause',
        icon: 'images/navigator_pause.png',
        scale: 'large'
    });
    navButtonPause.addListener('click', buttonPause);

    navButtonPrevious = new Ext.Button({ renderTo: 'buttonPrevious',
        icon: 'images/navigator_previous.png',
        scale: 'large'
    });
    navButtonPrevious.addListener('click', buttonPrevious);

    navButtonStop = new Ext.Button({ renderTo: 'buttonStop',
        icon: 'images/navigator_stop.png',
        scale: 'large'
    });
    navButtonStop.addListener('click', buttonStop);

    navButtonNext = new Ext.Button({ renderTo: 'buttonNext',
        icon: 'images/navigator_next.png',
        scale: 'large'
    });
    navButtonNext.addListener('click', buttonNext);

    navButtonNext = new Ext.Button({ renderTo: 'buttonGUI',
        icon: 'images/gui_control.png',
        scale: 'large'
    });
    navButtonNext.addListener('click', showGuiControl);

}

function InitializeSharesTree() {

    sharesTree = new Tree.TreePanel({
        el: 'center1',
        animate: true,
        enableDrag: true,
        containerScroll: true,
        rootVisible: true,
        dropConfig: { appendOnly: true }
    });

    // set the root node
    sharesRoot = new Tree.TreeNode({
        text: 'Music Shares',
        draggable: false, // disable root node dragging
        id: 'sharesRoot'
    });
    sharesTree.setRootNode(sharesRoot);

    sharesTree.addListener('click', expandnodeMusic);

    // render the tree
    sharesTree.render();

    sharesRoot.expand(false, false);

    sharesVideoTree = new Tree.TreePanel({
        el: 'center2',
        animate: true,
        //loader: new Tree.TreeLoader(),
        enableDrag: true,
        containerScroll: true,
        rootVisible: true,
        dropConfig: { appendOnly: true }
    });

    // set the root node
    sharesVideoRoot = new Tree.TreeNode({
        text: 'Video Shares',
        draggable: false, // disable root node dragging
        id: 'sharesRoot'
    });
    sharesVideoTree.setRootNode(sharesVideoRoot);

    sharesVideoTree.addListener('click', expandnodeVideo);

    // Right mouse click
    sharesVideoTree.on('contextmenu', function(node, e) {
        //handlePlayListContextMenu(node, e);
    });

    // render the tree
    sharesVideoTree.render();

    sharesVideoRoot.expand(false, /*no anim*/false);

}

function InitializePlaylistArea() {

    navButtonClearPlaylist = new Ext.Button({
        renderTo: 'playlistArea',
        scale: 'medium',
        text: 'Clear Playlist'
    });
    navButtonClearPlaylist.addListener('click', buttonClearPlayList);

    playlistTree = new Tree.TreePanel({
        el: 'playlistArea',
        animate: true,
        //loader: new Tree.TreeLoader(),
        enableDD: true,
        containerScroll: true,
        rootVisible: true,
        dropConfig: { appendOnly: false }
    });

    // set the root node
    playlistRoot = new Tree.TreeNode({
        text: 'Playlist',
        draggable: false, // disable root node dragging
        id: 'playlistRoot'
    });
    playlistTree.setRootNode(playlistRoot);

    // Left Mouse click.
    playlistTree.addListener('click', playSelectedSong);

    playlistTree.on('beforenodedrop', function(e) {
        var n = e.dropNode; // the node that was dropped
        var copy = new Ext.tree.TreeNode( // copy it
          Ext.apply({}, n.attributes));

        if (n.ownerTree.el.id != "myPlaylist") {
            addDirectoryContent(n.attributes.data, 'music', true);
            e.dropNode = null; // assign the copy as the new dropNode
        }
    });

    playlistTree.on('movenode', function(e) {
        xbmcCmds('ClearPlayList', xbmcCmdSuccess,clearPlayListFailure);

        var children = playlistRoot.childNodes;
        for (var i = 0; i < children.length; i++) {
            xbmcCmds('AddToPlayList(' + children[i].attributes.id + ')', xbmcCmdSuccess, addToPlayListFailure);
        }
        updatePlaylistTree();
    });

    // Right mouse click
    playlistTree.on('contextmenu', function(node, e) {
        //handlePlayListContextMenu(node, e);
    });

    // render the tree
    playlistTree.render();

    playlistRoot.expand(false, /*no anim*/false);

}

function clearPlayListFailure(t) {
    alert('clearPlayListFailure t:' + t);
}

function addToPlayListFailure(t) {
    alert('clearPlayListFailure t:' + t);
}

/********************* SetPlaylistType ***********************/

function setPlaylistType(type) {
    currentPlaylist = type;
    var inputUrl = '/xbmcCmds/xbmcHttp?command=SetCurrentPlaylist(' + type + ')';
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        success: setPlaylistTypeSuccess,
        failure: setPlaylistTypeFailure
    });
}

function getShares(type) {
    var successFunction = (type == 'music') ? getMusicSharesSuccess : getVideoSharesSuccess;
    var inputUrl = '/xbmcCmds/xbmcHttp?command=GetShares(' + type + ')';
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        success: successFunction,
        failure: getSharesFailure
    });
}

function setPlaylistTypeSuccess(t) {
    getShares('music');
    getShares('video');
}

function setPlaylistTypeFailure(t) {
    albumArt.src = "images/defaultAlbumCover.png";
}

/********************* getSharesSuccess ***********************/

function getMusicSharesSuccess(t) {

    var responseArr = t.responseText.replace(/<html>/g, "");
    responseArr = responseArr.replace(/<\/html>/g, "");
    responseArr = responseArr.replace(/\n/g, '');
    responseArr = responseArr.split("<li>");

    for (var i = 0; i < responseArr.length; i++) {
        if (responseArr[i] != "" && responseArr[i].length > 3) {
            aContentPaths = responseArr[i].replace(new RegExp("/", "g"), "\\");
            aContentPaths = aContentPaths.replace(/\n/g, '');
            responseArr2 = aContentPaths.split(";");
            newNode = new Tree.TreeNode({
            text: responseArr2[0],           
            draggable: false, // disable root node dragging
            data: responseArr2[1],
            id: nodeId,
            expanded: true,
                icon: "images/folder.gif"
            });

            newNode.on("click", function(node, e) {
                if (!node.attributes.id != "sharesRoot") {
                    getDirectoryInfo(node);
                } 
            }, this);
            sharesRoot.appendChild(newNode);
            nodeId++;
            newNode.expand(false, false);
            newNode.select();
        }
    }

    updatePlaylistTree();
    GetCurrentlyPlaying();
    getVolume();
}

function getVideoSharesSuccess(t) {

    var newNode = null;

    var responseArr = t.responseText.replace(/<html>/g, "");
    responseArr = responseArr.replace(/<\/html>/g, "");
    responseArr = responseArr.replace(/\n/g, '');
    responseArr = responseArr.split("<li>");

    for (var i = 0; i < responseArr.length; i++) {
        if (responseArr[i] != "" && responseArr[i].length > 3) {
            aContentPaths = responseArr[i].replace(new RegExp("/", "g"), "\\");
            aContentPaths = aContentPaths.replace(/\n/g, '');
            responseArr2 = aContentPaths.split(";");
            newNode = new Tree.TreeNode({
                text: responseArr2[0],
                draggable: false, // disable root node dragging
                id: nodeId,
                data: responseArr2[1],
                icon: "images/folder.gif"
            });

            sharesVideoRoot.appendChild(newNode);
            nodeId++;
        }
    }

    updatePlaylistTree();
    GetCurrentlyPlaying();
    getVolume();
}

function getSharesFailure(t) {
    alert('getSharesFailure failure t:' + t);
}

/********************* getDirectoryPaths ***********************/

function getDirectoryInfo(branch) {
    currentNodeSelected = branch.attributes.id;
    getDirectoryPaths(branch.attributes.data);
}

function getDirectoryPaths(url) {
    var inputUrl = '/xbmcCmds/xbmcHttp?command=GetDirectory(' + url + ')';
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        success: getDirectorySuccess,
        failure: getDirectoryFailure,
        timeout: 2000
    });
}

function getDirectorySuccess(t) {
    var struct1;
    var newBranch;
    var branch;
    var testBranch;
    var newNode = null;
    var branchRoot = null;

    var responseDirectory = t.responseText.replace(/<html>/g, "");
    responseDirectory = responseDirectory.replace(/<\/html>/g, "");
    responseDirectory = responseDirectory.replace(/\n/g, '');
    responseDirectory = responseDirectory.split("<li>");

    if (currentPlaylist == 'music')
        branchRoot = sharesTree.getNodeById(currentNodeSelected);
    else
        branchRoot = sharesVideoTree.getNodeById(currentNodeSelected);


    if (branchRoot == undefined || branchRoot == null) {
        alert('failure t:' + t.responseText);
    }

    if (branchRoot.hasChildNodes())
        return;

    for (var i = 0; i < responseDirectory.length; i++) {

        aContentPaths = responseDirectory[i].replace(new RegExp("/", "g"), "\\");
        aContentPaths = aContentPaths.replace(/\n/g, '');
        aTmpContent = aContentPaths.split("\\");
        if (aTmpContent.length > 1) {
            if (aContentPaths.charAt(aContentPaths.length - 1) == "\\") {
                aContentNames = aTmpContent[aTmpContent.length - 2];
                struct1 = { 'id': responseDirectory[i], 'txt': aContentNames };
                newNode = new Tree.TreeNode({
                text: struct1.txt,
                    id: nodeId,
                    data: struct1.id,
                    enableDrag: true,
                    icon: "images/folder.gif"
                });
            } else {
                aContentNames = aTmpContent[aTmpContent.length - 1];
                struct1 = { 'id': responseDirectory[i], 'txt': aContentNames };
                newNode = new Tree.TreeNode({
                    text: struct1.txt,
                    id: nodeId,
                    data: struct1.id,
                    enableDrag: true,
                    leaf: true
                });
            }
            if (aContentNames != undefined && aContentNames != "Error:Not folder") {
                branchRoot.appendChild(newNode);
                nodeId++;
            }
        }
        branchRoot.expand(false, false);
    }

    branchRoot.expand();
}

function getDirectoryFailure(t) {
    alert('getDirectoryFailure t:' + t);
}

/********************* updatePlaylistTree ***********************/

function updatePlaylistTree() {
    var inputUrl = '/xbmcCmds/xbmcHttp?command=GetPlaylistContents(GetCurrentPlaylist)';
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        success: updatePlaylistTreeSuccess,
        failure: updatePlaylistTreeFailure,
        timeout: 2000
    });
}

function updatePlaylistTreeSuccess(t) {

    var newNode = null;
    var tempIndex = 0;

    // Remove all children from root (delete the playlist).
    while (playlistRoot.hasChildNodes()) {
        playlistRoot.removeChild(playlistRoot.item(0));
    }

    var responsePlayList = t.responseText.replace(/<html>/g, "");
    responsePlayList = responsePlayList.replace(/<\/html>/g, "");
    responsePlayList = responsePlayList.replace(/\\n/g, '');
    responsePlayList = responsePlayList.split("<li>");

    for (var i = 0; i < responsePlayList.length; i++) {
        aContentPaths = responsePlayList[i].replace(new RegExp("/", "g"), "\\");
        aContentPaths = aContentPaths.replace(/\n/g, '');
        aTmpContent = aContentPaths.split("\\");

        if (aTmpContent.length > 1) {
            aContentNames = aTmpContent[aTmpContent.length - 1];
            struct1 = { 'id': responsePlayList[i], 'txt': aContentNames, 'img': 'page.gif' };

            if (aContentNames != undefined && aContentNames != "Error:Not folder") {
                newNode = new Tree.TreeNode({
                    text: struct1.txt,
                    id: nodeId,
                    data: struct1.id,
                    enableDrag: true,
                    leaf: true,
                    index: tempIndex
                });
                playlistRoot.appendChild(newNode);
                nodeId++;
                tempIndex++;
            }
        }
    }
    playlistRoot.expand(false, /*no anim*/false);
}

function updatePlaylistTreeFailure(t) {
    alert('updatePlaylistTreeFailure t:' + t);
}

/********************* addDirectoryContent ***********************/

function addDirectoryContentFailure(t) {
    alert('addDirectoryContentFailure t:' + t);
}

function addDirectoryContent(folderPath, mask, recursive) {
    p = "";
    m = "";
    r = "";

    if (mask != null) {
        m = ";[" + mask + "]";
        p = ";0";
        r = (recursive) ? ";1" : ";0";
    }

    var inputUrl = '/xbmcCmds/xbmcHttp?command=AddToPlayList(' + folderPath + p + m + r + ')';
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        success: updatePlaylistTree,
        failure: addDirectoryContentFailure,
        timeout: 2000
    });
}

/********************* Misc commands ***********************/

function xbmcCmdsFailure(t) {
    alert('xbmcCmds failure t:' + t);
}

function xbmcCmdSuccess(t) {
    //alert('xbmcCmds success t:' + t);
}

function xbmcCmds(command, callbackFunctionSuccess, callbackFunctionFailure) {
    if (connectStatus != 'Connected')
        return;
        
    var inputUrl = '/xbmcCmds/xbmcHttp?command=' + command;
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        success: function(t) {
            callbackFunctionSuccess(t.responseText);
        },
        failure: callbackFunctionFailure,
        timeout: 2000
    });
}

function playSelectedSong(branch) {
    //var currentSong = branch.attributes.data;
    //xbmcCmds('PlayFile(' + currentSong + ')', xbmcCmdSuccess, playSelectedSongFailure);
    var currentSongIndex = branch.attributes.index;
    xbmcCmds('SetPlaylistSong(' + currentSongIndex + ')', xbmcCmdSuccess, playSelectedSongFailure);
}

function playSelectedSongFailure(t) {
    alert('playSelectedSongFailure failure t:' + t);
}

function buttonSetVolume(volumeLevel) {
    xbmcCmds('ExecBuiltIn&parameter=SetVolume(' + volumeLevel + ')', xbmcCmdSuccess, buttonsSetVolumeFailure);
    GetCurrentlyPlaying();
}

function buttonsSetVolumeFailure(t) {
    alert('buttonsSetVolumeFailure failure t:' + t);
}

function buttonPlay() {
    if (currentlyPlaying.Filename == '[Nothing Playing]' || currentlyPlaying.Filesize == -1)
        playSelectedSong(playlistRoot.item(0));
    else
        xbmcCmds('ExecBuiltIn&parameter=PlayerControl(Play)', xbmcCmdSuccess, buttonPlayFailure);
    GetCurrentlyPlaying();
}

function buttonPlayFailure(t) {
    alert('buttonPlayFailure failure t:' + t);
}

function buttonPause() {
    xbmcCmds('ExecBuiltIn&parameter=PlayerControl(Play)', xbmcCmdSuccess, buttonPauseFailure);
    GetCurrentlyPlaying();
}

function buttonPauseFailure(t) {
    alert('buttonPauseFailure failure t:' + t);
}

function buttonPrevious() {
    xbmcCmds('ExecBuiltIn&parameter=PlayerControl(Previous)', xbmcCmdSuccess, buttonPreviousFailure);
    GetCurrentlyPlaying();
}

function buttonPreviousFailure(t) {
    alert('buttonPreviousFailure failure t:' + t);
}

function buttonNext() {
    xbmcCmds('ExecBuiltIn&parameter=PlayerControl(Next)', xbmcCmdSuccess, buttonNextFailure);
    GetCurrentlyPlaying();
}

function buttonNextFailure(t) {
    alert('buttonNextFailure failure t:' + t);
}

function buttonStop() {
    xbmcCmds('ExecBuiltIn&parameter=PlayerControl(Stop)', xbmcCmdSuccess, buttonStopFailure);
    GetCurrentlyPlaying();
}

function buttonStopFailure(t) {
    alert('buttonStopFailure failure t:' + t);
}

function buttonSetVolume(volumeLevel) {
    xbmcCmds('ExecBuiltIn&parameter=SetVolume(' + volumeLevel + ')', xbmcCmdSuccess, buttonSetVolumeFailure);
    GetCurrentlyPlaying();
}

function buttonSetVolumeFailure(t) {
    alert('buttonSetVolumeFailure failure t:' + t);
}


function buttonAddToPlayList() {
    if (currentNodeSelected != null) {
        addDirectoryContent(currentNodeSelected, 'music', true);
        GetCurrentlyPlaying();
    }
}

function buttonClearPlayList() {
    xbmcCmds('ClearPlayList', xbmcCmdSuccess, buttonClearPlayListFailure);
    updatePlaylistTree();
    GetCurrentlyPlaying();
}

function buttonClearPlayListFailure(t) {
    alert('buttonClearPlayListFailure failure t:' + t);
}

/********************* GetCurrentlyPlaying ***********************/

function GetCurrentlyPlaying() {
    var inputUrl = '/xbmcCmds/xbmcHttp?command=GetCurrentlyPlaying';
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        success: getCurrentlyPlayingSuccess,
        failure: getCurrentlyPlayingFailure,
        timeout: 2000
    });
}

function getCurrentlyPlayingSuccess(t) {

    responseArr = t.responseText;
    responseArr = t.responseText.split("<li>");

    var templocation = null;
    for (i = 0; i < responseArr.length; i++) {

        responseArr2 = responseArr[i].split(":");

        if (responseArr2.length > 2) {
            elementName = responseArr2[0];
            responseArr2 = responseArr2.slice(1);
            elementValue = responseArr2.join(":");

            currentlyPlaying[elementName] = elementValue;

        }
        else {
            if (responseArr2.length >= 2) {
                responseArr2[1] = responseArr2[1].replace(/\\n/g, '');
                responseArr2[1] = responseArr2[1].replace(/<\/html>/, '');
                responseArr2[1] = responseArr2[1].replace(/\n/g, '');
                currentlyPlaying[responseArr2[0]] = responseArr2[1];
            }
        }
    }

    if (currentlyPlaying.Title != undefined && currentlyPlaying.Filename != '[Nothing Playing]') {

        if (currentlyPlaying.Album == undefined) {
            currentlyPlaying.Album = "";
        }

        if (currentlyPlaying.Artist == undefined) {
            currentlyPlaying.Artist = "";
        }

        currentlyPlayingText.innerHTML = "<b>" + currentlyPlaying.Title + "<br>  " + currentlyPlaying.Album + "</b><br>" + currentlyPlaying.Artist + "<br> </div>" + currentlyPlaying.Time + " / " + currentlyPlaying.Duration + "</div>";

        justFilePath = currentlyPlaying.Filename.split("\\");
        justFilePath = justFilePath.slice(0, -1);
        justFilePath = justFilePath.join("\\");


        //Only download image once
        if (currentlyPlaying.Filename != lastTitle) {
            getThumbnail(justFilePath, 'albumArt');

            childNode = playlistRoot.findChild('data', lastTitle);
            if (childNode != null) {
                childNodeUI = childNode.getUI();
                childNodeUI.getIconEl().src = "images/default/tree/leaf.gif";
            }

            childNode = playlistRoot.findChild('data', currentlyPlaying.Filename);
            if (childNode != null) {
                childNodeUI = childNode.getUI();
                childNodeUI.getIconEl().src = "images/pointer.gif";
            }

        }
        lastTitle = currentlyPlaying.Filename;

        if (songProgressBar != null && songProgressBar != undefined) {
            var percentage = parseInt(currentlyPlaying.Percentage);
            songProgressBar.updateProgress(percentage / 100, currentlyPlaying.Percentage + '%');
        }
    }
    else {
        currentlyPlayingText.innerHTML = "Nothing Playing";
        albumArt.src = "images/defaultAlbumCover.png"        
    }
    connectStatus = 'Connected';
    connectionStatus.innerHTML = connectStatus;

    pollInProgress = false;

}

function getCurrentlyPlayingFailure(t) {
    connectStatus = t.statusText;
    connectionStatus.innerHTML = connectStatus;
}

/********************* getThumbnail ***********************/

function getThumbnail(filePath, returnDiv) {
    var tempUrl = currentlyPlaying.Thumb;
    var inputUrl = '/xbmcCmds/xbmcHttp?command=filedownload&parameter=' + tempUrl;
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        success: getThumbSuccess,
        failure: getThumbFailure,
        timeout: 2000
    });
}

function getThumbSuccess(t) {
    var temp = t.responseText.replace(/<html>/g, "");
    temp = temp.replace(/<\/html>/, "");

    if (temp.length > 10)
        albumArt.src = "data:image/jpeg;base64," + temp;
    else
        albumArt.src = "images/defaultAlbumCover.png";
}

function getThumbFailure(t) {
    albumArt.src = "images/defaultAlbumCover.png";
}

/********************* Sliders ***********************/

Ext.ux.SliderTip = Ext.extend(Ext.Tip, {
    minWidth: 10,
    offsets: [0, -10],
    init: function(slider) {
        slider.on('dragstart', this.onSlide, this);
        slider.on('drag', this.onSlide, this);
        slider.on('dragend', this.hide, this);
        slider.on('destroy', this.destroy, this);
    },

    onSlide: function(slider) {
        this.show();
        this.body.update(this.getText(slider));
        this.doAutoWidth();
        this.el.alignTo(slider.thumb, 'b-t?', this.offsets);
    },

    getText: function(slider) {
        return slider.getValue();
    }
});

/********************* getVolume ***********************/

function getVolume() {
    var inputUrl = '/xbmcCmds/xbmcHttp?command=GetVolume';
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        success: getVolumeSuccess,
        failure: getVolumeFailure,
        timeout: 2000
    });
}

function getVolumeSuccess(t) {
    var responseArr = t.responseText.replace(/<html>/g, "");
    responseArr = responseArr.replace(/<\/html>/g, "");
    responseArr = responseArr.replace(/\\n/g, '');
    responseArr = responseArr.replace(/<li>/g, '');
    currentVolume = parseInt(responseArr);
    soundSlider.setValue(currentVolume, false);
    if (currentVolume == 0) {
        isMuted = true;
    }
    else {
        isMuted = false;
    }
}

function getVolumeFailure(t) {
    alert('getVolumeFailure t:' + t);
}

/********************* guiControl ***********************/

function showGuiControl() {
    var ele = document.getElementById("guiControl");
    ele.style.display = "block";
}

function hideGuiControl() {
    var ele = document.getElementById("guiControl");
    ele.style.display = "none";
}

function SendKey(keyToSend) {
    var inputUrl = '/xbmcCmds/xbmcHttp?command=Action(' + keyToSend + ')';
    Ext.Ajax.request({
        url: inputUrl,
        method: 'GET',
        onSuccess: function(t) {
            //alert(t.responseText);
        },
        onFailure: updatePlaylistTreeFailure,
        timeout: 2000
    });
}
